package com.suqiu.common.utils;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.suqiu.common.exception.SuqiuServiceException;

import java.util.ArrayList;
import java.util.List;

public class PhoneVerifyUtils {
    private static final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

    /**
     * 是否是国内手机号
     * @param phone 带有 +国家编号 的手机号
     * @return
     */
    public static boolean isChinaNum(String phone) throws NumberParseException {
        if (!isPhoneValid(phone)) {
            throw new SuqiuServiceException("不存在该手机号");
        }
        Phonenumber.PhoneNumber number = phoneNumberUtil.parse(phone, Phonenumber.PhoneNumber.CountryCodeSource.UNSPECIFIED.name());
        String regionCodeForNumber = phoneNumberUtil.getRegionCodeForNumber(number);
        return "CN".equals(regionCodeForNumber);
    }

    /**
     * 是否存在该手机号
     *
     * @param phone 带有 +国家编号 的手机号
     * @return
     * @throws NumberParseException
     */
    public static boolean isPhoneValid(String phone) throws NumberParseException {
        // 校验是否存在该手机号
        Phonenumber.PhoneNumber number = phoneNumberUtil.parse(phone, Phonenumber.PhoneNumber.CountryCodeSource.UNSPECIFIED.name());
        return phoneNumberUtil.isValidNumber(number);
    }

    public static void main(String[] args) throws NumberParseException {
        //TW:+886938003563  香港：+85263930525
        System.out.println(isChinaNum("+18182708950"));

    }

}